﻿<!--#include file="../../lib/base.asp"-->
<!--#include file="../../theme/admin/config.asp"-->
<!--#include file="../../lib/cmd.asp"-->
<%
''' 自定义系统参数插件
''' ==================================================================
''' 版权所有 http://www.ahseo.cc
''' ------------------------------------------------------------------
''' 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
''' 未经授权不允许对程序代码以任何形式任何目的的再发布。
''' ==================================================================
''' 插件编写: 寻云
''' 开发日期：2014.01

	'注释：插件是否安装
	is_plug_install "sdcms.plug.dimuser"
	
	'注释：管理权限检查
	is_plug_login
	
	'注释：定义插件模板路径
	dim plug_theme,plug_theme_add,plug_theme_edit
	plug_theme="module/plug_dimuser/list.html"
	plug_theme_add="module/plug_dimuser/add.html"
	plug_theme_edit="module/plug_dimuser/edit.html"
	
	'接收fget传值
	dim act:act=lcase(sdcms.fget("act",0))
	dim id:id=sdcms.getint(sdcms.fget("id",0),0)
	dim data
			
	select case act
		case "del":deldb
		case "add"
			load plug_theme_add
		case "adddb":adddb	
		case "edit"
			data=sdcms.db.dbload(1,"dimuserName,dimuserTitle,dimuserType,dimusermode,fdatatype,flength,dimuserValue,foptions,IsMust,frules,ordnum,isshow,isreg,isdata","sd_plug_dimuser","id="&id&"","")
			load plug_theme_edit
		case "editdb":editdb
		case "order":ordernum	
		case "del":deldb
		case else
			load plug_theme
	end select	

	'保存新增字段
	sub adddb()
		dim t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t20,t21
		t0=sdcms.enhtml(sdcms.fpost("t0",0))
		t1=sdcms.enhtml(sdcms.fpost("t1",0))
		t2=sdcms.getint(sdcms.fpost("t2",0),0)
		t3=sdcms.getint(sdcms.fpost("t3",0),0)
		t4=sdcms.getint(sdcms.fpost("t4",0),0)
		t5=sdcms.getint(sdcms.fpost("t5",0),0)
		t6=sdcms.enhtml(sdcms.fpost("t6",0))
		t7=sdcms.enhtml(sdcms.fpost("t7",0))
		t8=sdcms.getint(sdcms.fpost("t8",0),0)
		t9=sdcms.getint(sdcms.fpost("t9",0),0)
		t11=sdcms.getint(sdcms.fpost("t11",0),0)
		t13=sdcms.getint(sdcms.fpost("t13",0),0)
		t20=sdcms.getint(sdcms.fpost("t20",0),0)
		t21=sdcms.getint(sdcms.fpost("t21",0),0)
		if sdcms.strlen(t0)=0 then sdcms.ajaxjson "参数名称不能为空",0:exit sub
		if sdcms.strlen(t1)=0 then sdcms.ajaxjson "参数标题不能为空",0:exit sub
		if sdcms.strlen(t2)=0 then sdcms.ajaxjson "参数类型不能为空",0:exit sub
		t0="my_"&t0
		if t8=0 then t9=0
		
			'字段类型
		dim dtype
		select case t2
			case "1"
				select case t3
					case "1","4":dtype="文本"
					case "2":dtype="数字"
					case "3":dtype="日期"
					case "5":dtype="货币"
				end select
			case "2","6","7"
				dtype="备注"
			case "11"
			dtype="日期"
			case else
				select case t4
					case "1":dtype="文本"
					'case "2":dtype="数字"
				end select
		end select
		t0=left(t0,20)
		dim addtime
		addtime=now()
		dim data
		data=array(array("dimuserName",t0,255,1),array("dimuserTitle",t1,255,1),array("dimuserType",t2,0,0),array("dimusermode",t3,1,0),array("fdatatype",t4,1,0),array("flength",t5,5,0),array("dimuserValue",t6,255,1),array("foptions",t7,0,1),array("IsMust",t8,1,0),array("frules",t9,1,0),array("ordnum",t11,255,0),array("isshow",t13,1,0),array("AddTime",addtime,50,1),array("isreg",t20,1,0),array("isdata",t21,1,0))
		if sdcms.db.dbnew("sd_plug_dimuser",data,"dimuserName='"&t0&"'")=0 then
			sdcms.ajaxjson "字段名称已存在，请换个试试",0:exit sub
		else
		'保存字段到dimuser表
		
		dim sqlstr
		select case dtype
			case "文本":sqlstr=""&t0&" nvarchar("&t5&") null"
			case "数字":sqlstr=""&t0&" int null"
			case "货币":sqlstr=""&t0&" money null"
			case "日期":sqlstr=""&t0&" datetime null"
			case "备注":sqlstr=""&t0&" ntext null"	
		end select
			sqlstr="alter table sd_user add "&sqlstr
			sdcms.db.exedb(sqlstr)
			sdcms.ajaxjson "保存成功",1
		end if

	end sub		
	
	'编辑
	sub editdb
		dim t1,t6,t7,t8,t9,t10,t11,t12,t13,t20,t21
		t1=sdcms.enhtml(sdcms.fpost("t1",0))
		t6=sdcms.enhtml(sdcms.fpost("t6",0))
		t7=sdcms.enhtml(sdcms.fpost("t7",0))
		t8=sdcms.getint(sdcms.fpost("t8",0),0)
		t9=sdcms.getint(sdcms.fpost("t9",0),0)
		t11=sdcms.getint(sdcms.fpost("t11",0),0)
		t13=sdcms.getint(sdcms.fpost("t13",0),0)
		t20=sdcms.getint(sdcms.fpost("t20",0),0)
		t21=sdcms.getint(sdcms.fpost("t21",0),0)
		if sdcms.strlen(t1)=0 then sdcms.ajaxjson "参数标题不能为空",0:exit sub
		if t8=0 then t9=0
		dim data,id,addtime,ddata
		addtime=now()
		id=sdcms.getint(sdcms.fget("id",0),0)
		data=array(array("dimuserTitle",t1,255,1),array("dimuserValue",t6,255,1),array("foptions",t7,0,1),array("IsMust",t8,1,0),array("frules",t9,1,0),array("ordnum",t11,255,0),array("isshow",t13,1,0),array("AddTime",addtime,50,1),array("isreg",t20,1,0),array("isdata",t21,1,0))
		if sdcms.db.dbupdate("sd_plug_dimuser","id="&id&"",data)=1 then
			sdcms.ajaxjson "保存成功",1
		end if
	end sub
	
	'排序
	sub ordernum()
		dim t0,t1,t2,t3,i
		t0=sdcms.enhtml(sdcms.fpost("fid",0))
		t1=sdcms.enhtml(sdcms.fpost("ordnum",0))
		t2=split(t0,",")
		t3=split(t1,",")
		if ubound(t2)-ubound(t3)<>0 then sdcms.ajaxjson "&#21442;&#25968;&#38169;&#35823;",0:exit sub
		for i=0 to ubound(t2)
			if sdcms.isnum(t2(i)) then
				sdcms.db.dbupdate "sd_plug_dimuser","ID="&t2(i)&"",array(array("ordnum",t3(i),10,0,0))
			end if
		next
		sdcms.ajaxjson "保存成功",1
	end sub
	
	'删除自定义字段
	sub deldb()
		dim fid:fid=sdcms.getint(sdcms.fget("fid",0),0)
		'查询字段ID、名称及所属模型ID
		dim fdata:fdata=sdcms.db.dbload(1,"id,dimusername","sd_plug_dimuser","id="&fid&"","")		
		'删除sd_user中的字段
		dim sqlstr:sqlstr="alter table sd_user drop "&fdata(1,0)
		sdcms.db.exedb(sqlstr)			
		'删除sd_plug_filed中的字段
		sdcms.db.dbdel "sd_plug_dimuser","id="&fid&""
				
		'打印处理结果		
		sdcms.echo "1&#21024;&#38500;&#25104;&#21151;"
	end sub
	
	'获取对应的字段类型
	function get_dimusertype(byval t0)
		if not isnumeric(t0) then get_dimusertype="参数不正确":exit function
		select case t0
			case "1":get_dimusertype="单行文本框"
			case "2":get_dimusertype="多行文本框"
			case "3":get_dimusertype="下拉列表"
			case "4":get_dimusertype="单选按钮"
			case "5":get_dimusertype="复选框"
			case "6":get_dimusertype="HTML编辑器"
			case "10":get_dimusertype="省市联动"
			case "11":get_dimusertype="日历选择"
		end select
	end function

	sdcms.db.dbclose
%>